import 'dart:async';

import 'package:qyou_app/common/base.dart';
import 'package:flutter/material.dart';

/*
 * 启动页倒计时组件
 */
class CountDown extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _CountDownState();
}

class _CountDownState extends State<CountDown> with Base {
  Timer _timer;
  int _currentTime = 0;

  @override
  void initState() {
    super.initState();
    _timer = Timer.periodic(Duration(milliseconds: 50), (timer) {
      _currentTime += 50;
      if (_currentTime >= 5000) {
        _toHomePage();
        _timer.cancel();
      }
      setState(() {});
    });
  }

  @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }

  //点击跳过倒计时
  void _toHomePage() {
    Navigator.of(context).pushReplacementNamed("/index");
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: _toHomePage,
      child: Container(
        alignment: Alignment.center,
        width: dp(50),
        height: dp(50),
        child: Column(
          children: [
            Stack(
              alignment: Alignment.center,
              children: [
                CircularProgressIndicator(
                  value: _currentTime / 5000,
                ),
                Text("${_currentTime ~/ 1000}s"),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
